package com.molichuxing.services.business.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.business.dto.request.create.StockBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.StockBizModifyDto;
import com.molichuxing.services.business.service.StockStoreBizService;
import com.molichuxing.services.infrastructure.dto.request.create.StockCreateDto;
import com.molichuxing.services.infrastructure.dto.request.create.StockStoreCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.StockModifyDto;
import com.molichuxing.services.infrastructure.dto.response.StockStoreDto;
import com.molichuxing.services.infrastructure.service.StockService;
import com.molichuxing.services.infrastructure.service.StockStoreService;
import com.molichuxing.services.property.StockTypeEnum;

/**
 * 
 * @author liyg
 * 库存物流组合服务实现类
 * 2019-08-21
 *
 */
@Service("stockStoreBizService")
public class StockStoreBizServiceImpl implements StockStoreBizService{
	
	@Autowired
	private StockStoreService stockStoreService;
	
	@Autowired
	private StockService stockService;

	/**
	 * 创建门店仓库
	 * 
	 * @param createDto
	 * @return
	 */
	@Override
	public Boolean create(StockBizCreateDto createDto) {
		
		StockCreateDto stockCreateDto=new StockCreateDto();
		BeanUtils.copyProperties(createDto, stockCreateDto);
		stockCreateDto.setType(StockTypeEnum.STORE.getValue());
		Integer id = stockService.create(stockCreateDto);
		if(null == id) {
			throw new RuntimeException();
		}
		
		StockStoreCreateDto stockStoreCreateDto=new StockStoreCreateDto();
		stockStoreCreateDto.setStockId(id);
		stockStoreCreateDto.setStoreId(createDto.getStoreId());
		
		Boolean flag = stockStoreService.create(stockStoreCreateDto);
		if(!flag) {
			throw new RuntimeException();
		}
		
		return flag;
	}
	
	/**
	 * 修改门店仓库
	 * 
	 * @param modify
	 * @return
	 */
	@Override
	public Boolean modify(StockBizModifyDto modifyDto) {
		
		StockStoreDto stockStoreDto = stockStoreService.getStockStoreByStoreId(modifyDto.getStoreId());
		if(null == stockStoreDto) {
			throw new BizException(101,"该门店没有仓库信息");
		}
		
		StockModifyDto stockModifyDto=new StockModifyDto();
		BeanUtils.copyProperties(modifyDto, stockModifyDto);
		stockModifyDto.setId(stockStoreDto.getStockId());
		
		Boolean flag = stockService.modify(stockModifyDto);
		if(!flag) {
			throw new RuntimeException();
		}
		
		return flag;
	}

}
